//
//  autoLoginViewController.m
//  FastRunDemo
//
//  Created by 苟亚明 on 16/10/25.
//  Copyright © 2016年 苟亚明. All rights reserved.
//

#import "autoLoginViewController.h"
#import <AFNetworking.h>
#import "rootTabBarViewController.h"
#import "userDefault.h"
#import "LoginViewController.h"
#import "KPNaviViewController.h"
@interface autoLoginViewController ()
@property (nonatomic, strong) rootTabBarViewController *rootTabBarViewController;
@end

@implementation autoLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [self goNext];
    });
}

- (void)goNext{
    
    // 登录到广场模块
    NSString *url = [NSString stringWithFormat:@"%@%@",GYMRequeatHost,GYMLogin];
    
    AFHTTPRequestOperationManager * manage = [AFHTTPRequestOperationManager manager];
    
    manage.requestSerializer = [AFHTTPRequestSerializer serializer];
    manage.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSDictionary *params = @{@"username":[userDefault getValueOfKey:@"userName"],@"password":[userDefault getValueOfKey:@"passWord"]};
    NSLog(@"%@%@",[userDefault getValueOfKey:@"userName"],[userDefault getValueOfKey:@"passWord"]);
    [manage POST:url parameters:params success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        rootTabBarViewController *vc = [rootTabBarViewController sharedRootVC];
        self.rootTabBarViewController = vc;
        self.rootTabBarViewController.navigationController.navigationBar.hidden = NO;

        self.view.window.rootViewController = self.rootTabBarViewController;
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {

        LoginViewController *vc = [[LoginViewController alloc]init];
        KPNaviViewController *Navi = [[KPNaviViewController alloc]initWithRootViewController:vc];
        self.view.window.rootViewController = Navi;
    }];
}
@end
